CMAKE_MINIMUM_REQUIRED(VERSION 2.8.12)


if(NOT EXISTS "${ock-dfs_SOURCE_DIR}/3rdparty/googletest/CMakeLists.txt")
  message(ERROR "submodule 3rdparty/json is missing. to fix try run: \n git submodule update --init")
  set(MISSING_INTERNAL_GTEST_LIBRARY 1)
endif()
if(NOT MISSING_INTERNAL_GTEST_LIBRARY)
  SET(GTEST_ROOT_DIR "${ock-dfs_SOURCE_DIR}/3rdparty/googletest")
  SET(GTEST_INCLUDE_DIRS ${GTEST_ROOT_DIR}/googletest/include )
endif()

INCLUDE(CMakeParseArguments)

FUNCTION(ADD_UNIT_TEST TEST_NAME)
  SET(ONE_VALUE_ARGS PREFIX)
  SET(MULTI_VALUE_ARGS FILES BUILD_FLAGS DEPENDS INCLUDE_PATHS LINK_LIBRARIES THIRDPARTY_DEPENDS)

  CMAKE_PARSE_ARGUMENTS(ADD_UNIT_TEST "" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN})

  SET(TEST_TARGET_NAME ut_${TEST_NAME}_test)

  ADD_EXECUTABLE(${TEST_TARGET_NAME} ${ADD_UNIT_TEST_FILES})

  IF(ADD_UNIT_TEST_INCLUDE_PATHS)
    TARGET_INCLUDE_DIRECTORIES(${TEST_TARGET_NAME} PUBLIC ${ADD_UNIT_TEST_INCLUDE_PATHS})
  ENDIF()

  IF(ADD_UNIT_TEST_LINK_LIBRARIES)
    TARGET_LINK_LIBRARIES(${TEST_TARGET_NAME} ${ADD_UNIT_TEST_LINK_LIBRARIES})
  ENDIF()

  IF(ADD_UNIT_TEST_DEPENDS)
    ADD_DEPENDENCIES(${TEST_TARGET_NAME} ${ADD_UNIT_TEST_DEPENDS})
  ENDIF()

  IF(ADD_UNIT_TEST_BUILD_FLAGS)
    TARGET_COMPILE_OPTIONS(${TEST_TARGET_NAME} PUBLIC ${ADD_UNIT_TEST_BUILD_FLAGS})
  ENDIF()

  TARGET_GTEST(${TEST_TARGET_NAME})
  ADD_TEST(NAME ${TEST_NAME} COMMAND $<TARGET_FILE:${TEST_TARGET_NAME}>)

  FOREACH(DEP ${ADD_UNIT_TEST_THIRDPARTY_DEPENDS})
      IF(${DEP} STREQUAL "GLOG")
          MESSAGE("Link target:glog for ${TEST_TARGET_NAME}")
          TARGET_GLOG(${TEST_TARGET_NAME})
      ENDIF()
  ENDFOREACH()
ENDFUNCTION()
